package com.lg.system.wxuser.controller;
import java.util.HashMap;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.lg.common.base.BaseController;
import com.lg.common.validate.Delete;
import com.lg.common.validate.Insert;
import com.lg.common.validate.Update;
import com.lg.system.wxgroup.entity.WxGroup;
import com.lg.system.wxgroup.service.WxGroupService;
import com.lg.utils.PageModel;
import com.lg.utils.Query;
import com.lg.utils.R;
import com.lg.wechat.business.wxuser.entity.WxUser;
import com.lg.wechat.business.wxuser.service.WxUserService;

import me.chanjar.weixin.common.exception.WxErrorException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dl
 * @since 2018-04-02
 */
@RestController
@RequestMapping("/wxuser")
public class WxUserController extends BaseController<WxUser, WxUserService> {

    private final String prefix="system/wxuser/";
    
	@Autowired
	WxUserService wxUserService;
	
	@Autowired
	WxGroupService wxGroupService;

	@RequiresPermissions("wxuser:wxuser")
	@GetMapping("/index")
	public ModelAndView index(){
		ModelAndView mView=new ModelAndView(prefix+"index");
		return mView;
	}
	
	@RequiresPermissions("wxuser:wxuser")
	@RequestMapping("/list")
	public PageModel selectList(@RequestBody WxUser  wxUser){
		Query<WxUser> query = new Query<WxUser>(wxUser);
		List<WxUser> wxUsers=wxUserService.selectList(query.getEntityWrapper());
		PageModel pa=new PageModel(wxUsers,query.getPage().getTotal());
		return pa;
	}
	
	@RequiresPermissions("wxuser:add")
	@GetMapping("/add")
	public ModelAndView add(){
		ModelAndView mView=new ModelAndView(prefix+"add");
		return mView;
	}
	
	@RequiresPermissions("wxuser:edit")
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Long id){
		ModelAndView mView=new ModelAndView(prefix+"edit");
		mView.addObject("obj", wxUserService.selectById(id));
		return mView;
	}
	
	@RequiresPermissions("wxuser:add")
	@PostMapping("/save")
	public R save(@RequestBody @Validated(value={Insert.class}) WxUser wxUser,BindingResult bindingResult){
		if(wxUserService.save(wxUser)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxuser:edit")
	@PostMapping("/update")
	public R update(@RequestBody @Validated(value={Update.class}) WxUser wxUser,BindingResult bindingResult){
		if(wxUserService.update(wxUser)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresPermissions("wxuser:del")
	@PostMapping("/deleteRow")
	public R deleteRow(@RequestBody @Validated(value={Delete.class}) WxUser wxUser,BindingResult bindingResult){
		
		if(wxUserService.deleteById(wxUser.getId())){
			return R.ok();
		}else{
			return R.error();
		}  
	}
	
	@RequiresPermissions("wxuser:del")
	@PostMapping("/deleteRows")
	public R deleteRows(@RequestParam(value="ids[]") List<Long> ids){
		if(wxUserService.deleteByIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@GetMapping("/move")
	public ModelAndView move(){
		List<WxGroup> list=wxGroupService.selectByMap(new HashMap<>());
		ModelAndView mView=new ModelAndView(prefix+"user_group");
		mView.addObject("list", list);
		return mView;
	}
	
	@PostMapping("/move")
	public R move(@RequestParam(value="ids[]") List<Long> ids,@RequestParam(value="gid[]") List<Long> gid) throws WxErrorException{
		if(wxUserService.move(ids,gid)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	
	@RequiresPermissions("wxuser:wxuser")
	@RequestMapping("/selectListByXml")
	public PageModel selectListByXml(@RequestBody WxUser  wxUser){
		Query<WxUser> query = new Query<WxUser>(wxUser);
		List<WxUser> wxUsers=wxUserService.selectListByXml(wxUser);
		PageModel pa=new PageModel(wxUsers,query.getPage().getTotal());
		return pa;
	}
	
	

}
